package com.jin.spirng_boot_learn.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class client {

    public static void main(String[] args) throws IOException {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //客户端请求与本机在20006端口建立TCP连接
                try (Socket client = new Socket("127.0.0.1", 8080)) {
                    client.setSoTimeout(100);
                    //获取键盘输入
                    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                    //获取Socket的输出流，用来发送数据到服务端
                    PrintStream out = new PrintStream(client.getOutputStream());
                    //获取Socket的输入流，用来接收从服务端发送过来的数据
                    BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    boolean flag = true;

                        System.out.println("1111111111111");
                        out.println("111111111111111111111111");
                        System.out.println("2222222222222");
                        Thread.sleep(1000*100);
                    if (client != null) {
                        //如果构造函数建立起了连接，则关闭套接字，如果没有建立起连接，自然不用关闭
                        client.close(); //只关闭socket，其关联的输入输出流也会被关闭
                         System.out.println("33333333333333333");
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        for(int i=0;i<10;i++) {
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}
